using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Innovator;

namespace InnovationGame
{
	public class Game1 : Microsoft.Xna.Framework.Game
	{
		GraphicsDeviceManager graphics;

		MouseDevice mouse;
		KeyboardDevice keyboard;
		FPSCamera cam;
		bool fired = false;
		Vector3 inputModifier;
		bool toggleWireframe=false;

		public Game1()
		{
			graphics = new GraphicsDeviceManager(this);
		}
		protected override void LoadContent()
		{
			Engine.SetupEngine(graphics);
			//ClearScreen clear = new ClearScreen();
			Camera camera = new FPSCamera();
			camera.Position = new Vector3(3, 3, 5);
			camera.Target = new Vector3(0, 0, 0);

			Engine.Services.AddService(typeof(Camera), camera);
			cam = (FPSCamera)Engine.Services.GetService<Camera>();
			mouse = new MouseDevice();
			Engine.Services.AddService(typeof(MouseDevice), mouse);
			keyboard = new KeyboardDevice();
			Engine.Services.AddService(typeof(KeyboardDevice), keyboard);

			Engine.Services.AddService(typeof(Physics),new Physics());
			//Actor actor = new Actor(Engine.Content.Load<Model>("Content/ig_box"), new Vector3(0, 0, 0));
			//actor.DrawOrder = 2;
			/*
			PhysicsActor plane = new PhysicsActor(
				Engine.Content.Load<Model>("Content/ig_plane"),
				new BoxObject(new Vector3(4, .01f, 4)));
			plane.PhysicsObject.Immovable = true;
			Model model = Engine.Content.Load<Model>("Content/ig_box");
			for (int y = 0; y < 3; ++y)
			{
				for (int x = 0; x < 3; ++x)
				{
					PhysicsActor act = new PhysicsActor(model, new BoxObject(
						new Vector3(.5f),
						new Vector3(-.5f + (x * 0.52f), .5f + (y * 0.52f), -1),
						Vector3.Zero));
					act.Scale = new Vector3(.5f);
				}
			}
			*/
			Terrain terrain = new Terrain(
				Engine.Content.Load<Texture2D>("Content/heightmap"),
				Engine.Content.Load<Texture2D>("Content/grass"));
		}
		protected override void Update(GameTime gameTime)
		{
			Engine.Update(gameTime);

			//mouse = Engine.Services.GetService<Mouse>();
			//keyboard = Eng
			//cam = (FPSCamera)Engine.Services.GetService<Camera>();

			inputModifier = new Vector3(
				(keyboard.IsKeyDown(Keys.A) ? -1 : 0) + (keyboard.IsKeyDown(Keys.D) ? 1 : 0),
				(keyboard.IsKeyDown(Keys.Q) ? -1 : 0) + (keyboard.IsKeyDown(Keys.E) ? 1 : 0),
				(keyboard.IsKeyDown(Keys.W) ? -1 : 0) + (keyboard.IsKeyDown(Keys.S) ? 1 : 0));

			cam.RotateTranslate(new Vector3(mouse.Delta.Y * -.002f,mouse.Delta.X*-.002f ,0),inputModifier * .5f);
			if (mouse.WasButtonPressed(MouseButtons.Left))
			{
				PhysicsActor act = new PhysicsActor(
					Engine.Content.Load<Model>("Content/ig_box"),
					new BoxObject(new Vector3(.5f), cam.Position, Vector3.Zero));
				act.Scale = new Vector3(.5f);
				act.PhysicsObject.Mass = 1000;
				Vector3 dir = cam.Target - cam.Position;
				act.PhysicsObject.Velocity = dir*10;
			}
			if (Engine.Services.GetService<KeyboardDevice>().WasKeyPressed(Keys.F1))
			{
				if (!toggleWireframe)
				{
					Engine.GraphicsDevice.RenderState.FillMode = FillMode.WireFrame;
				}
				else
				{
					Engine.GraphicsDevice.RenderState.FillMode = FillMode.Solid;
				}
				toggleWireframe = !toggleWireframe;
			}
			
			if (Engine.Services.GetService<KeyboardDevice>().IsKeyDown(Keys.Escape))
				this.Exit();


			/*if (gameTime.TotalGameTime.TotalSeconds > 2 && !fired)
			{
				PhysicsActor act = new PhysicsActor(
					Engine.Content.Load<Model>("Content/ig_box"),
					new BoxObject(new Vector3(1), new Vector3(0, .5f, 1), Vector3.Zero));
				act.Scale = new Vector3(1);
				act.PhysicsObject.Mass = 1000;
				act.PhysicsObject.Velocity = new Vector3(0, 2, -6);
				fired = true;
			}*/

			base.Update(gameTime);
		}
		protected override void Draw(GameTime gameTime)
		{
			Engine.Draw(gameTime, ComponentType.All);
			base.Draw(gameTime);
		}
	}
}
